package com.bjpowernode.crm.workbench.service.impl;

import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.workbench.domain.Activity;
import com.bjpowernode.crm.workbench.mapper.ActivityMapper;
import com.bjpowernode.crm.workbench.service.ActivityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 郭俊才
 * 2021/8/22
 */
@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public int saveSelective(Activity activity) {
        //设置主键
        activity.setId(UUIDUtils.getUUID());
        //设置创建时间
        activity.setCreateTime(DateUtils.formatDateTime(new Date()));
        return activityMapper.insertSelective(activity);
    }

    @Override
    public int removeList(String[] ids) {

        return activityMapper.deleteList(ids);
    }

    @Override
    public List<Activity> queryAll() {
        return activityMapper.selectAll();
    }

    @Override
    public int saveForList(List<Activity> list) {
        Activity activity = null;
        for (int i = 0; i < list.size(); i++) {
            activity = list.get(i);
            //设置主键
            activity.setId(UUIDUtils.getUUID());
            //设置创建时间
            activity.setCreateTime(DateUtils.formatDateTime(new Date()));
        }

        return activityMapper.insertForList(list);
    }

    @Override
    public Activity queryByPrimaryKeyForDetails(String id) {
        return activityMapper.selectByPrimaryKeyForDetails(id);
    }

    @Override
    public int editByPrimaryKeySelective(Activity activity) {
        //设置修改时间
        activity.setEditTime(DateUtils.formatDateTime(new Date()));
        //设置修改人

        return activityMapper.updateByPrimaryKeySelective(activity);
    }

    @Override
    public Activity queryByPrimaryKey(String id) {
        return activityMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<Activity> queryByConditions(Activity activity, Integer pageNum, Integer pageSize) {
        //开启pagehelper分页
        PageHelper.startPage(pageNum, pageSize);
        //查询数据
        List<Activity> activityList = activityMapper.selectByConditions(activity);
        //将数据集合传入pageinfo的list中，并返回一个pageinfo对象
        return new PageInfo<Activity>(activityList);
    }

    @Override
    public List<Activity> queryByIds(String[] ids) {
        return activityMapper.selectByIds(ids);
    }

    @Override
    public List<Activity> queryByNameAndClueId(String activityName, String clueId) {
        return activityMapper.selectByNameAndClueId(activityName, clueId);
    }
    @Override
    public List<Activity> queryByName(String activityName) {
        return activityMapper.selectByName(activityName);
    }

    @Override
    public List<Activity> queryActivityByCluePrimaryKey(String id) {
        return activityMapper.selectActivityByCluePrimaryKey(id);
    }
}
